<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<#--<mapper namespace="${package.Mapper}.${table.mapperName}">-->
<mapper namespace="${package.Mapper}.${table.mapperName}">
    <#if enableCache>
        <!-- 开启二级缓存 -->
        <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>

    </#if>
    <#if baseResultMap>
        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
            <#list table.fields as field>
                <#if field.keyFlag><#--生成主键排在第一位-->
                    <id column="${field.name}" property="${field.propertyName}"/>
                </#if>
            </#list>
            <#list table.commonFields as field><#--生成公共字段 -->
                <result column="${field.name}" property="${field.propertyName}"/>
            </#list>
            <#list table.fields as field>
                <#if !field.keyFlag><#--生成普通字段 -->
                    <result column="${field.name}" property="${field.propertyName}"/>
                </#if>
            </#list>
        </resultMap>

    </#if>
    <#if baseColumnList>
        <!-- 通用查询结果列 -->
        <sql id="Base_Column_List">
            <#list table.commonFields as field>
                ${field.name},
            </#list>
            ${table.fieldNames}
        </sql>
    </#if>
        <select id="selectList" resultMap="BaseResultMap">
            select
            <include refid="Base_Column_List"/>
            from ${table.name}
            <where>
                <#list table.fields as field>
                    <#if !field.keyFlag>
                        <#if field.type?contains("date")||field.type?contains("time")>
                            <if test="${field.name} != null"> and ${field.name} = ${cfg.left}${field.name}${cfg.right}</if>
                        <#else>
                            <if test="${field.name} != null  and ${field.name} != ''"> and ${field.name}
                                = ${cfg.left}${field.name}${cfg.right}</if>
                        </#if>
                    </#if>
                </#list>
            </where>
        </select>

<#--    <select id="list" resultMap="BaseResultMap">-->
<#--        select-->
<#--        <include refid="Base_Column_List"/>-->
<#--        from ${table.name}-->
<#--        where create_time=${cfg.left}create_time} and user_key=${cfg.left}user_key}-->

<#--        <if test="sort_info !=null and sort_info !=''">-->
<#--            order by ${cfg.left}sort_info}-->
<#--            <if test="sort_type !=null and sort_type !=''">-->
<#--                <if test="sort_type=='DESC'">-->
<#--                    DESC-->
<#--                </if>-->
<#--                <if test="sort_type=='ASC'">-->
<#--                    ASC-->
<#--                </if>-->
<#--            </if>-->
<#--        </if>-->
<#--    </select>-->

<#--    <select id="listByHistory" parameterType="${package.Entity}.${entity}" resultMap="BaseResultMap">-->
<#--        select * from(-->
<#--        select * from ${table.name}-->
<#--        where user_key=${cfg.left}user_key}-->

<#--        group by-->
<#--        )b-->
<#--        <if test="sort_info !=null and sort_info !=''">-->
<#--            order by ${cfg.left}sort_info}-->
<#--            <if test="sort_type !=null and sort_type !=''">-->
<#--                <if test="sort_type=='DESC'">-->
<#--                    DESC-->
<#--                </if>-->
<#--                <if test="sort_type=='ASC'">-->
<#--                    ASC-->
<#--                </if>-->
<#--            </if>-->
<#--        </if>-->
<#--    </select>-->

<#--    <select id="selectCount" resultType="com.cocam.erp.modules.salesanalysis.pojo.DaySaleCount">-->
<#--        select-->
<#--        sum(yesterday_sale_num)sale_num_count,-->
<#--        sum(yesterday_saleroom)saleroom_count,-->
<#--        sum(before_yesterday_sale_num)before_yesterday_sale_num_count,-->
<#--        sum(before_yesterday_saleroom)before_yesterday_saleroom_count-->
<#--        from ${table.name}-->
<#--        where create_time=${cfg.left}create_time} and user_key=${cfg.left}user_key}-->


<#--    </select>-->

<#--    <select id="selectCountByHistory" resultType="com.cocam.erp.modules.salesanalysis.pojo.DaySaleCount">-->
<#--        select-->
<#--        sum(yesterday_sale_num)sale_num_count,-->
<#--        sum(yesterday_saleroom)saleroom_count-->
<#--        from ${table.name}-->
<#--        where user_key=${cfg.left}user_key}-->

<#--        <if test="start_time !=null and start_time!=''">-->
<#--            and create_time &gt;= ${cfg.left}start_time}-->
<#--        </if>-->
<#--        <if test="end_time !=null and end_time!=''">-->
<#--            and create_time &lt;= ${cfg.left}end_time}-->
<#--        </if>-->
<#--    </select>-->

<#--    <select id="selectCountDetailByHistory" resultMap="BaseResultMap">-->
<#--        select-->
<#--        <include refid="Base_Column_List"/>-->
<#--        from ${table.name}-->
<#--        where  user_key=${cfg.left}user_key}-->

<#--        <if test="start_time !=null and start_time!=''">-->
<#--            and create_time &gt;= ${cfg.left}start_time}-->
<#--        </if>-->
<#--        <if test="end_time !=null and end_time!=''">-->
<#--            and create_time &lt;= ${cfg.left}end_time}-->
<#--        </if>-->
<#--    </select>-->

<#--    <select id="selectTrend" resultMap="BaseResultMap">-->
<#--        select DATE_FORMAT(create_time,-->
<#--        <if test="time_type == 0">'%Y-%m-%d'</if>-->
<#--        <if test="time_type == 1">'%Y-%u'</if>-->
<#--        <if test="time_type == 2">'%Y-%m'</if>-->
<#--        <if test="time_type == 3">'%w'</if>-->
<#--        ) times,-->
<#--        sum(yesterday_sale_num)yesterday_sale_num,-->
<#--        sum(yesterday_saleroom)yesterday_saleroom-->
<#--        from ${table.name}-->
<#--        where  user_key=${cfg.left}user_key}-->

<#--        <if test="start_time !=null and start_time!=''">-->
<#--            and create_time &gt;= ${cfg.left}start_time}-->
<#--        </if>-->
<#--        <if test="end_time !=null and end_time!=''">-->
<#--            and create_time &lt;= ${cfg.left}end_time}-->
<#--        </if>-->
<#--        group by DATE_FORMAT(create_time,-->
<#--        <if test="time_type == 0">'%Y%m%d'</if>-->
<#--        <if test="time_type == 1">'%Y%u'</if>-->
<#--        <if test="time_type == 2">'%Y%m'</if>-->
<#--        <if test="time_type == 3">'%w'</if>-->
<#--        ),-->
<#--    </select>-->
        <select id="getById" resultType="${package.Entity}.${entity}">
            select
            <include refid="Base_Column_List"/>
            from ${table.name}
            <where>
                id = ${cfg.left}id${cfg.right}
            </where>
        </select>
        <update id="update" parameterType="${package.Entity}.${entity}">
            update ${table.name}
            <set>
                <#list table.fields as field>
                    <#if !field.keyFlag>
                        <#if field.type?contains("date")||field.type?contains("time")>
                            <if test="${field.name} != null">${field.name}=${cfg.left}${field.name}${cfg.right},</if>
                        <#else>
                            <if test="${field.name} != null and ${field.name} != ''">${field.name}
                                =${cfg.left}${field.name}${cfg.right},
                            </if>
                        </#if>
                    </#if>
                </#list>
            </set>
            where id =${cfg.left}id${cfg.right}
        </update>
        <insert id="insert" parameterType="${package.Entity}.${entity}">
            insert into ${table.name}
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <#list table.fields as field>
                    <#if !field.keyFlag>
                        <#if field.type?contains("date")||field.type?contains("time")>
                            <if test="${field.name} != null">${field.name},</if>
                        <#else>
                            <if test="${field.name} != null and ${field.name} != ''">${field.name},</if>
                        </#if>
                    </#if>
                </#list>
            </trim>
            values
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <#list table.fields as field>
                    <#if !field.keyFlag>
                        <#if field.type?contains("date")||field.type?contains("time")>
                            <if test="${field.name} != null">${cfg.left}${field.name}${cfg.right},</if>
                        <#else>
                            <if test="${field.name} != null and ${field.name} != ''">${cfg.left}${field.name}${cfg.right},
                            </if>
                        </#if>
                    </#if>
                </#list>
            </trim>
        </insert>
        <delete id="delete">
            delete from ${table.name} where id=${cfg.left}id${cfg.right}
        </delete>
</mapper>